package VN.EDU.IMIC.PROJECTLEADER.NGUYENVANDOAN.XML_DEMO;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.swing.JFileChooser;
import javax.swing.filechooser.FileNameExtensionFilter;

import vn.edu.imic.XmlProcess.XMLManagements;

public class READXMLFILES {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // 1. Khai báo các datafield của lớp đối tượng
        List<Object> lisField = new ArrayList<Object>();
        lisField.add("Id");
        lisField.add("RollNo");
        lisField.add("FullName");
        lisField.add("Email");
        lisField.add("Phone");
        lisField.add("Mark");

        // 2. Khai báo & khởi tạo đối tượng của thư viện
        XMLManagements objManager = new XMLManagements();

        // 3. Khai báo 01 đối tượng List<List<HashMap>>
        try {
            
            JFileChooser objFile = new JFileChooser();
            objFile.setCurrentDirectory(new java.io.File("."));
            objFile.setDialogTitle("Chọn file: ");
            objFile.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
            String[] EXTENSION = new String[] { "xml" };
            FileNameExtensionFilter filter = new FileNameExtensionFilter("XML files", EXTENSION);
            objFile.setFileFilter(filter);
            objFile.setMultiSelectionEnabled(false);

            int returnVal = objFile.showOpenDialog(null);
            String ParentPath = "";
            String FileName = "";
            if (returnVal == JFileChooser.APPROVE_OPTION) {
                ParentPath = objFile.getSelectedFile().getParent();
                FileName = objFile.getSelectedFile().getName();
            }
            
            /*List<List<HashMap<String, String>>> lisKQ = 
            objManager.XMLOfItems(lisField, "Student", "C:\\Students.xml");*/
            
            List<List<HashMap<String, String>>> lisKQ = 
            objManager.XMLOfItems(lisField, "Student", FileName);
            
            // 4. Duyệt danh sách list HashMap để hiển thị ra ngoài màn hình
            HashMap<String, String> objHash = null;
            for (int i = 0; i < lisKQ.size(); i++)
                for (int j = 0; j < lisKQ.get(i).size(); j++) {
                    objHash = lisKQ.get(i).get(j);
                    System.out.println("Id : " + objHash.get("Id"));
                    System.out.println("RollNo: " + objHash.get("RollNo"));
                    System.out.println("FullName: " + objHash.get("FullName"));
                    System.out.println("Email: " + objHash.get("Email"));
                    System.out.println("Phone: " + objHash.get("Phone"));
                    System.out.println("Mark: " + objHash.get("Mark"));
                    System.out.println();
                }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
